/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.accordion;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQAccordionUtils
{

	/**
	 * Create a JQAccordionBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQAccordionBuilder
	 */
	public static JQAccordionBuilder create(String id, String key)
	{
		return new JQAccordionBuilder(new JQAccordion(id, (key == null ? JQAccordionPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQAccordionBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQAccordionBuilder
	 */
	public static JQAccordionBuilder create(String id)
	{
		return new JQAccordionBuilder(new JQAccordion(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQAccordionBuilder
	{
		/** JQuery component */
		private JQAccordion component;
		/** JQuery default options */
		private JQAccordionOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQAccordionBuilder(JQAccordion component)
		{
			this.component = component;
			this.options = new JQAccordionOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the active option value
		 * 
		 * @param active
		 *            accordion active
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder active(String active)
		{
			options.setActive(active);
			return this;
		}

		/**
		 * Sets the active option value
		 * 
		 * @param active
		 *            accordion active
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder active(Boolean active)
		{
			options.setActive(active);
			return this;
		}

		/**
		 * Sets the active option value
		 * 
		 * @param active
		 *            accordion active
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder active(Integer active)
		{
			options.setActive(active);
			return this;
		}

		/**
		 * Sets the animated option value
		 * 
		 * @param animated
		 *            accordion animated
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder animated(String animated)
		{
			options.setAnimated(animated);
			return this;
		}

		/**
		 * Sets the animated option value
		 * 
		 * @param animated
		 *            accordion animated
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder animated(Boolean animated)
		{
			options.setAnimated(animated);
			return this;
		}

		/**
		 * Sets the selected class option value
		 * 
		 * @param selectedClass
		 *            accordion selectedClass
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder selectedClass(String selectedClass)
		{
			options.setSelectedClass(selectedClass);
			return this;
		}

		/**
		 * Sets the event option value
		 * 
		 * @param event
		 *            accordion event
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder event(String event)
		{
			options.setEvent(event);
			return this;
		}

		/**
		 * Sets the header option value
		 * 
		 * @param header
		 *            accordion header
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder header(String header)
		{
			options.setHeader(header);
			return this;
		}

		/**
		 * Sets the navigation filter option value
		 * 
		 * @param navigationFilter
		 *            accordion navigationFilter
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder navigationFilter(String navigationFilter)
		{
			options.setNavigationFilter(navigationFilter);
			return this;
		}

		/**
		 * Sets the alwaysOpen option value
		 * 
		 * @param alwaysOpen
		 *            accordion alwaysOpen
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder alwaysOpen(boolean alwaysOpen)
		{
			options.setAlwaysOpen(alwaysOpen);
			return this;
		}

		/**
		 * Sets the navigation option value
		 * 
		 * @param navigation
		 *            accordion navigation
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder navigation(boolean navigation)
		{
			options.setNavigation(navigation);
			return this;
		}

		/**
		 * Sets the autoHeight option value
		 * 
		 * @param autoHeight
		 *            accordion autoHeight
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder autoHeight(boolean autoHeight)
		{
			options.setAutoHeight(autoHeight);
			return this;
		}

		/**
		 * Sets the clearStyle option value
		 * 
		 * @param clearStyle
		 *            accordion clearStyle
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder clearStyle(boolean clearStyle)
		{
			options.setClearStyle(clearStyle);
			return this;
		}

		/**
		 * Sets the fillSpace option value
		 * 
		 * @param fillSpace
		 *            accordion fillSpace
		 * @return JQAccordionBuilder
		 */
		public JQAccordionBuilder fillSpace(boolean fillSpace)
		{
			options.setFillSpace(fillSpace);
			return this;
		}

		/**
		 * @return JQAccordion
		 */
		public JQAccordion build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}